package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealOverViewVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    @Transactional
    public void save(SetmealDTO setmealDTO) {
        //新增套餐表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);

        //新增套餐里的菜品表数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        Long mealId = setmeal.getId();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(mealId));
        setmealDishMapper.insert(setmealDishes);
    }

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.page(setmealPageQueryDTO);
        long total = page.getTotal();
        List<SetmealVO> result = page.getResult();
        return new PageResult(total,result);
    }

    @Transactional
    public void deleteByIds(List<Long> ids) {
        //在售状态下，不可删除套餐
        for (Long id : ids) {
            if(setmealMapper.getById(id).getStatus()==0){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        //删除套餐
        setmealMapper.deleteByIds(ids);

        //删除当前套餐下的菜品
        setmealDishMapper.deleteBySetmealIds(ids);
    }

    @Transactional
    public void update(SetmealDTO setmealDTO) {
        //修改套餐信息（除套餐内菜品）
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);

        //修改套餐菜品
        //先将菜品全部删除
        List<Long> setmealIds = new ArrayList<>();
        Long setmealId = setmealDTO.getId();
        setmealIds.add(setmealId);
        setmealDishMapper.deleteBySetmealIds(setmealIds);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //设置套餐菜品的套餐ID，否则无法正常插入
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealId));
        //再进行插入
        setmealDishMapper.insert(setmealDishes);
    }

    @Override
    public SetmealVO getById(Long id) {
        SetmealVO setmealVO = new SetmealVO();

        //添加套餐内容
        Setmeal setmeal = setmealMapper.getById(id);
        BeanUtils.copyProperties(setmeal,setmealVO);

        //添加套餐分类名称


        //添加套餐内菜品
        List<SetmealDish> setmealDishes = setmealDishMapper.getSetmealDishesBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }

    @Override
    public void startOrStop(Integer status,Long id) {
        //根据status与id获取setmeal对象
        Setmeal setmeal = Setmeal.builder().id(id).status(status).build();

        //套餐内如果有停售菜品，则套餐无法上架

        //如果要起售套餐，查询套餐内菜品状态
        if(status == StatusConstant.ENABLE){
            //判断套餐内是否有停售菜品
            List<SetmealDish> setmealDishes = setmealDishMapper.getSetmealDishesBySetmealId(id);
            if(setmealDishes != null && !setmealDishes.isEmpty())
            {
                for (SetmealDish setmealDish : setmealDishes) {
                    //根据每一个dishId查询获取dish对象
                    Dish dish = dishMapper.getById(setmealDish.getDishId());
                    //判断dish对象的状态是否为停售
                    if(dish.getStatus()==StatusConstant.DISABLE){
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                }
            }
        }

        //正常情况，可以更新状态
        setmealMapper.update(setmeal);
    }

    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        return setmealMapper.list(setmeal);
    }

    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealDishMapper.getDishItemBySetmealId(id);
    }
}
